<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        #outer {
            width: 520px;
            height: 400px;
            margin: 0 auto;
            padding: 20px 0;
            background-color: yellow;
            margin-top: 100px;
            position: relative;
            overflow: hidden;
        }

        #imgList {
            position: absolute;
            left: 0;
        }

        li img {
            width: 500px;
            height: 400px;
        }

        #imgList li {
            float: left;
            margin: 0 10px;
        }

        #nav {
            position: absolute;
            bottom: 5px;
            left: 200px;
        }

        #nav a {
            float: left;
            width: 10px;
            height: 10px;
            background-color: red;
            margin: 0 5px;
            opacity: 0.5;
        }

        #nav a:hover {
            background-color: black;
        }
    </style>
    <script src="./toos.js"></script>
    <script>
        window.onload = function () {
            //设置imglist的宽度
            var imgList = document.getElementById("imgList");
            var imgarr = document.getElementsByTagName("img");
            imgList.style.width = 520 * imgarr.length + "px";
            var index = 0;

            var allA = document.getElementsByTagName("a");
            allA[index].style.backgroundColor = "black";

            for (var i = 0; i < allA.length; i++) {
                allA[i].num = i;
                allA[i].onclick = function () {

                    //关闭自动切换定时器
                    clearTimeout(timer);
                    index = this.num;
                    setA();
                    move(imgList, "left", -520 * index, 20,function(){
                        autoChange();
                    });
                }
            }
            autoChange();

            function setA() {
                //判断当前索引是否是最后一张图片
                if (index >= imgarr.length - 1) {
                    index = 0;
                    imgList.style.left=0;
                }
                for (var i = 0; i < allA.length; i++) {
                    allA[i].style.backgroundColor = "";
                };
                allA[index].style.backgroundColor = "black";
            };
            var timer;
            //创建一个函数，用来开启自动切换图片
            function autoChange() {
                //开启一个定时器，自动切换图片
                timer=setInterval(function () {
                    //使索引自增
                    index++;
                    index %= imgarr.length;
                    move(imgList, "left", -520 * index, 20, function () {
                        //修改导航点
                        setA();
                    });
                }, 1000);
            };
        };

    </script>
</head>

<body>
    <!-- 创建一个外部的div,来作为大的容器 -->
    <div id="outer">
        <!-- 创建一个ul,用于放置图片 -->
        <ul id="imgList">
            <li>
                <img src="./img2/1.jpg" alt="" />
            </li>
            <li>
                <img src="./img2/2.jpg" alt="" />
            </li>
            <li>
                <img src="./img2/3.jpg" alt="" />
            </li>
            <li>
                <img src="./img2/4.jpg" alt="" />
            </li>
            <li>
                <img src="./img2/5.jpg" alt="" />
            </li>
            <li>
                <img src="./img2/1.jpg" alt="" />
            </li>
        </ul>
        <div id="nav">
            <a href="javascript:;"></a>
            <a href="javascript:;"></a>
            <a href="javascript:;"></a>
            <a href="javascript:;"></a>
            <a href="javascript:;"></a>
        </div>
    </div>

</body>

</html>